package com.draven.order.controller;


import com.draven.model.ResponseResult;
import com.draven.model.product.ProductDTO;
import com.draven.order.entity.Order;
import com.draven.order.properties.OrderProperties;
import com.draven.order.service.OrderService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderProperties orderProperties;

    @Resource
    private OrderService orderService;

    @GetMapping("/getOrderProperties")
    public ResponseResult<String> getOrder() {
        return ResponseResult.success(orderProperties.getName() + ":" + orderProperties.getUrl());
    }

    @GetMapping("/create/{id}/{num}")
   // @SentinelResource(value = "create", fallback = "createFallback")
    public ResponseResult<String> create(@PathVariable("id") Long id, @PathVariable("num") Integer num) {
       try {
           return ResponseResult.success(orderService.create(id, num));
       }catch (Exception e){
          return ResponseResult.error(500, "Fallback: " + e.getMessage());
       }
    }

    @GetMapping("/getOrderList")
    public ResponseResult<List<Order>> getOrderList(){
        return ResponseResult.success(orderService.getOrderList());
    }

    @GetMapping("/getOrderById/{id}")
    public ResponseResult<Order> getOrderById(@PathVariable("id") Long id) {
        return ResponseResult.success(orderService.getOrderById(id));
    }

    @PutMapping("/updateOrder/{id}")
    public ResponseResult<String> updateOrder(@PathVariable("id") Long id, @RequestBody Order order) {
        return ResponseResult.success(orderService.updateOrder(id, order));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteOrder(@PathVariable("id") Long id) {
        try {
            orderService.deleteOrder(id);
            return ResponseResult.success("Order deleted successfully");
        } catch (Exception e) {
            return ResponseResult.error(500, "Error deleting order: " + e.getMessage());
        }
    }

    @DeleteMapping("/deleteBatch")
    public ResponseResult<String> deleteOrdersBatch(@RequestBody List<Long> ids) {
        try {
            orderService.deleteOrdersBatch(ids);
            return ResponseResult.success("Orders deleted successfully");
        } catch (Exception e) {
            return ResponseResult.error(500, "Error deleting orders: " + e.getMessage());
        }
    }

    @GetMapping("/product/page")
    public ResponseResult<List<ProductDTO>> productPage() {
        return ResponseResult.success(orderService.productPage());
    }

    @GetMapping("/product/detail/{id}")
    public ResponseResult<ProductDTO> productDetail(@PathVariable("id") Long id) {
        return ResponseResult.success(orderService.productDetail(id));
    }

    // Fallback method for Sentinel
    public ResponseResult<String> createFallback(Long id, Integer num, Throwable ex) {
        return ResponseResult.error(500, "Fallback: " + ex.getMessage());
    }
}